package com.htfyun.fragment.App;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Debug;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.htfyun.AppInfo;
import com.htfyun.IScanOverListener;
import com.htfyun.ScanInstalledAppManager;
import com.htfyun.fragment.base.BaseWithPageNumberAdapter;
import com.htfyun.fragment.base.BaseWithPageNumberFragment;
import com.htfyun.utils.DebugLog;
import com.htfyun.view.CustomGridLayoutManager;
import com.htfyun.view.GridSpacingItemDecoration;

import org.geometerplus.zlibrary.ui.android.R;

import java.util.Collections;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sst on 2017/11/27.
 */

public class AppFragment extends BaseWithPageNumberFragment<AppInfo> {

    private static final int SPAN_COUNT = 6;

    public static AppFragment newInstance() {

        Bundle args = new Bundle();

        AppFragment fragment = new AppFragment();
        fragment.setArguments(args);

        return fragment;
    }

    private IScanOverListener scanOverListener = new IScanOverListener() {
        @Override
        public void onOver() {
            if (getActivity() != null) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        configUI();

                    }
                });
            }
        }
    };

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        configUI();

        ScanInstalledAppManager.INSTANCE.addOverListener(scanOverListener);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ScanInstalledAppManager.INSTANCE.removeOverListener(scanOverListener);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();

    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(_mActivity, SPAN_COUNT);

    }

    @Override
    public RecyclerView.ItemDecoration getDecor() {
        int space = getContext().getResources().getDimensionPixelSize(R.dimen.library_img_cover_space);
        return (new GridSpacingItemDecoration(SPAN_COUNT, space, true));
    }

    @Override
    public BaseWithPageNumberAdapter getAdapter() {
        return new AppAdapter(_mActivity);
    }

    @Override
    public int getItemCountPerPage() {
        return getContext().getResources().getInteger(R.integer.app_count_per_page);
    }

    @Override
    public List<AppInfo> getTotalItemList() {
        if (!ScanInstalledAppManager.INSTANCE.isScanOver()) {

            return Collections.emptyList();
        }

        return ScanInstalledAppManager.INSTANCE.getAppInfoList();
    }

    @Override
    public String getStringForTextEmpty() {
        return "";
    }


    private ProgressDialog progressDialog;

    protected void showProgressDialog() {
        if (progressDialog != null) {
            return;
        }
        dismissProgressDialog();
        String title = "Loading";//mContext.getString(titleId);
        String message = "Please wait...";//getContext().getString(messageId);
        progressDialog = ProgressDialog.show(getContext(), title, message, false, false);
    }



    protected void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
